package app.server.project.main.service.db.entity.user;

import app.server.project.core.constants.Def;
import app.server.project.core.syndb.SynDBMgr;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "user_luck_order")
@TableName("user_luck_order")
@Comment(value = "用户抽奖订单")
public class UserLuckOrderEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;

    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;


    @TableField("success_time")
    @Comment("订单到账时间")
    @Column(name = "success_time")
    private LocalDateTime successTime;

    @Transient
    @TableField(exist = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss'(UTC)'")
    private LocalDateTime successTimeStr;

    @TableField("success_unix_time")
    @Comment("订单到账时间锉")
    @Column(name = "success_unix_time")
    private long successUnixTime;

    @TableField("choice")
    @Comment("字母选择")
    @Column(name = "choice")
    private String choice;

    @TableField("hash")
    @Comment("哈希值")
    @Column(name = "hash")
    private String hash;

    @TableField("param")
    @Comment("对应参数")
    @Column(name = "param")
    private long param;

    @TableField("high_val")
    @Comment("区块高度")
    @Column(name = "high_val")
    private long highVal;

    @TableField("user_id")
    @Comment("用户id")
    @Column(name = "user_id")
    @JsonSerialize(using = ToStringSerializer.class)
    private long userId;

    @TableField("status")
    @Comment("状态 0:创建订单 1:已经处理")
    @Column(name = "status")
    private int status;


    @Comment("0 安慰奖 1 一等奖 2 二等奖")
    @Column(name = "reward_type")
    private int rewardType;


    @TableField("order_type")
    @Comment("1:btc 2:ton")
    @Column(name = "order_type")
    private int orderType;
    @TableField("ton_val")
    @Comment("ton值")
    @Column(name = "ton_val")
    private long tonVal;
    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public static UserLuckOrderEntity of(long orderId) {
        UserLuckOrderEntity userEnergyEntity = new UserLuckOrderEntity();
        userEnergyEntity.setId(orderId);
        userEnergyEntity.setSynDb(true);
        userEnergyEntity.setStatus(Def.Zero);
        userEnergyEntity.setCreateTime(LocalDateTime.now());
        return userEnergyEntity;
    }

    public void setTonVal(long tonVal) {
        this.tonVal = tonVal;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getTonVal, this);
    }

    public void setSuccessUnixTime(long successUnixTime) {
        this.successUnixTime = successUnixTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getSuccessUnixTime, this);
    }

    public void setRewardType(int rewardType) {
        this.rewardType = rewardType;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getRewardType, this);
    }

    public void setSuccessTime(LocalDateTime successTime) {
        this.successTime = successTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getSuccessTime, this);
    }

    public void setOrderType(int orderType) {
        this.orderType = orderType;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getOrderType, this);
    }

    public void setUserId(long userId) {
        this.userId = userId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getUserId, this);
    }

    public void setChoice(String choice) {
        this.choice = choice;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getChoice, this);
    }

    public void setHash(String hash) {
        this.hash = hash;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getHash, this);
    }

    public void setHighVal(long highVal) {
        this.highVal = highVal;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getHighVal, this);
    }

    public void setStatus(int status) {
        this.status = status;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getStatus, this);
    }


    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getCreateTime, this);
    }

    public void setParam(long param) {
        this.param = param;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserLuckOrderEntity::getParam, this);
    }


}
